#!/bin/bash
##mariadb数据库安装脚本
##更新时间：2018年11月22日 22:09:23
##By:雅宸
read -p "是否需要新建数据库普通用户及数据库[建议使用普通用户建站]
[y/n] " newuser\n
read -p "是否允许数据库root用户远程登陆
[y/n] " long\n
read -p "请输入数据库密码[root]" pass \n
if [ $newuser == y ];then
	read -p "请输入数据库用户[新建一个用户]" dbuser
	read -p "请输入$dbuser的密码" dbpass
	read -p "请输入数据库名[新用户使用的数据库]" dbname
else
	echo  emmmm
fi
echo -e '你的数据库信息如下，空值表示未配置该项'
#sh configure/variable/database-display.sh  #显示数据库信息
echo -e " 数据库信息如下
-------------------------------------------------
---root密码    $dbpass                                   ---
---新用户:$dbuser 新用户密码:$dbpassFORblog         ---
---新建数据库名称： $dbname        ----------
---     $dbnameFORblog                  ---
--------------------------------------
"

echo --------------------------------
echo -e '确认无误请按回车'
read HC
echo -e '正在安装相关软件，请耐心等待'
yum install -y -q mariadb mariadb-server python2-PyMySQL expect 
echo -e '数据库安装完成'
systemctl daemon-reload
service=mariadb
sh configure/Detection/service-start.sh
sh configure/Detection/service-enable.sh
sh configure/Detection/service-start.sh
if [ -z "$start" ];then
		sh configure/install/db/mariadbInitialization.sh  ##初始化数据库
		echo -e '正在配置数据库'
		if [ $long == y ];then
			mysql -uroot -p$pass -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$pass' WITH GRANT OPTION;"
			mysql -uroot -p$pass -e "flush privileges;"
		else
			echo 
		fi
		if [ $newuser == y ];then
			mysql -uroot -p$pass -e "create database $dbname;"
			mysql -uroot -p$pass -e "create user $dbuser ;"
			mysql -uroot -p$pass -e "flush privileges;"
			echo  ——————-----------------------正在重新启动数据库服务-----------------------
			systemctl restart mariadb.service
			mysql -uroot -p$pass -e "GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass' WITH GRANT OPTION;"
			mysql -uroot -p$pass -e "GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'%' IDENTIFIED BY '$dbpass' WITH GRANT OPTION;"
			mysql -uroot -p$pass -e "flush privileges;"
		else
			echo  
		fi
		echo  ——————-----------------------正在重新启动数据库服务-----------------------
		sh configure/Detection/service-start.sh
		echo -e '数据库配置完成'
else
		echo -e '数据库服务启动失败'
fi
